/**
 * Created by Julia on 2016/05/03.
 */
define(['jquery', 'domReady!', 'appLocalStorage', 'appConst', 'model/global', 'business/user/userConfig', 'appMessageBox', 'jquery-form/jquery-form.min'], function ($, domReady, appLocalStorage, appConst, global, userConfig, msgBox) {
    return {
        initEditUser: function () {$("#user-form").validate({
            // Rules for form validation
            rules:userConfig.rules,
            messages:userConfig.messages,
            // Ajax form submition
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    success: function (d) {
                        if(d.code == 0) {
                            if (d.message == "EXCEPTION_GLOBAL_OBJECT_ALREADY_EXISTS") {
                                msgBox.errorBox(d.message, $("#loginName").val());
                            } else {
                                msgBox.errorBox(d.message, 'MENU_SYSTEM_USER');
                            }
                            return false;
                        }

                        $("#user-form").addClass('submited');
                    },
                    error: function(e){
                        if (e.status == 401) {
                            msgBox.sessionFailureBox();
                        }
                        if (e.status == 500) {
                            msgBox.serverFailureBox();
                        }
                        if (e.status == 404) {
                            msgBox.pathFailureBox();
                        }
                    }
                });
            },
            // Do not change code below
            errorPlacement: function (error, element) {
                error.insertAfter(element.parent());
            }
        });

        },
        initEvent: function () {
            var status = $('#status').val();
            $('.u-status').each(function () {
                var status02 = $(this).attr('data-value');
                if (status02 == status) {
                    $(this).addClass($(this).attr('data-status'));
                }
            });

            $('.u-status').click(function (e) {
                $('.u-status').each(function () {
                    $(this).removeClass($(this).attr('data-status'));
                });
                $(this).addClass($(this).attr('data-status'));
                $('#status').val($(this).attr('data-value'));
            });

            $('#lk_addnew').click(function () {
                $('#user-form').removeClass('submited');
                $('#user-form')[0].reset();
                e.preventDefault();
            });
            //$.formbar();
        }
    }
});

